package com.itheima.service.impl;

import com.itheima.mapper.ReportServiceMapper;
import com.itheima.mapper.ReprotMapper;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.support.DateData;
import com.itheima.utils.DateUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@DubboService
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReprotMapper reprotMapper;
    @Autowired
    private ReportServiceMapper reportServiceMapper;
    @Autowired
    private MemberService memberService;
    @Override
    public Map businessExcel() {
        Map reportData = new HashMap();
        reportData.put("reportDate",DateUtil.formatDate(new Date(),"yyyy-MM-dd"));
        int totalMember = memberService.count();
        reportData.put("totalMember",totalMember);
        List<Integer> memberList= reportServiceMapper.findMemberCounts(DateUtil.formatDate(new Date(),"yyyy-MM-dd"),DateUtil.getWeekNum(new Date()));
        System.out.println(memberList);
        reportData.put("todayNewMember",memberList.get(0));
        reportData.put("thisWeekNewMember",memberList.get(1));
        reportData.put("thisMonthNewMember",memberList.get(2));

        List<Integer> orderStatus1 = reportServiceMapper.findOrderCounts(DateUtil.formatDate(new Date(),"yyyy-MM-dd"),DateUtil.getWeekNum(new Date()) ,1);
        System.out.println(orderStatus1);
        reportData.put("todayOrderNumber",orderStatus1.get(0));
        reportData.put("thisWeekOrderNumber",orderStatus1.get(1));
        reportData.put("thisMonthOrderNumber",orderStatus1.get(2));

        List<Integer> orderStatus2 = reportServiceMapper.findOrderCounts(DateUtil.formatDate(new Date(),"yyyy-MM-dd"),DateUtil.getWeekNum(new Date()) ,2);
        reportData.put("todayVisitsNumber",orderStatus2.get(0));
        reportData.put("thisWeekVisitsNumber",orderStatus2.get(1));
        reportData.put("thisMonthVisitsNumber",orderStatus2.get(2));

        List<Map> hotSetmeal = reportServiceMapper.findHotSetmeal();
        reportData.put("hotSetmeal",hotSetmeal);
        return reportData;
    }

    @Override
    public Map findMemberCountByDate(DateData dateData)  {
        List<Map> mapList = reportServiceMapper.findMemberCountByDate(dateData.getStartDateStr(),dateData.getEndDateStr());
        return changeMapListToMap(mapList);
    }

    public Map changeMapListToMap(List<Map> mapList) {
        Map map = new HashMap();
        List<String> yearAndMonth = new ArrayList<>();
        List<Long> memberCounts = new ArrayList<>();
        for (Map map1 : mapList) {
            memberCounts.add((long) map1.get("totalCounts"));
            yearAndMonth.add((String) map1.get("yearAndMonth"));
        }
        map.put("yearAndMonth",yearAndMonth);
        map.put("totalCounts",memberCounts);
        return map;
    }

    @Override
    public List<Map> memberAgeProportion() {
        return reprotMapper.memberAexProportion();
    }

    @Override
    public List<Map> memberSexProportion() {
        return reprotMapper.memberSexProportion();
    }


}




